package com.yc.bos.dao.base;

import com.yc.bos.dao.base.IBaseDao;
import com.yc.bos.model.PageBean;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class BaseDaoImpl<T> implements IBaseDao<T> {
    private Class<T> entityClass;

    public BaseDaoImpl() {
        System.out.println("公共Dao实现类的空参构造方法");
        ParameterizedType p = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type[] types = p.getActualTypeArguments();
        entityClass = (Class<T>) types[0];
    }

    @Autowired
    protected HibernateTemplate hibernateTemplate;

    @Override
    public void save(T entity) {
        this.hibernateTemplate.save(entity);
    }

    @Override
    public void update(T entity) {
        this.hibernateTemplate.update(entity);
    }

    @Override
    public List<T> findAllByDetachedCriteria(DetachedCriteria dc) {
        return this.hibernateTemplate.findByCriteria(dc);
    }

    @Override
    public void delete(T entity) {

        this.hibernateTemplate.delete(entity);
    }


    @Override
    public T findById(Serializable id) {
        return this.hibernateTemplate.get(entityClass, id);
    }

    @Override
    public void executeUpdate(String hql, Object... objs) {
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
       Query query= session.createQuery(hql);
       for(int i=0;i<objs.length;i++){
           query.setParameter(i,objs[i]);
       }
       query.executeUpdate();

    }

    @Override
    public void executeUpdateByQueryName(String queryName, Object... objs) {
        Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query=session.getNamedQuery(queryName);
        System.out.println("queryName3--"+ queryName);
        for(int i=0;i<objs.length;i++){
            query.setParameter(i,objs[i]);
        }
        query.executeUpdate();

    }

    @Override
    public void saveAll(List<T> list) {
        hibernateTemplate.saveOrUpdateAll(list);

    }

    @Override
    public void pageQuery(PageBean<T> pd) {
        DetachedCriteria dc=pd.getDetachedCriteria();
        dc.setProjection(Projections.rowCount());
        List<Long> list=hibernateTemplate.findByCriteria(dc);
        Long total=list.get(0);
        pd.setTotal(total.intValue());
        dc.setProjection(null);
        int start=(pd.getCurrentPage()-1)*pd.getPageSize();
        int length=pd.getPageSize();
        List<T> rows=hibernateTemplate.findByCriteria(dc,start,length);
        pd.setRows(rows);
    }

    @Override
    public List<T> findAll() {
        String hql = "from " +entityClass.getSimpleName();
        System.out.println("BaeDaoImpl hql:"+hql);
        return this.hibernateTemplate.find(hql);
    }

}
